import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(12, 8))


def housePlotView(df):
	counts = df.groupby(by="户型").count()  # 统计
	plt.figure(facecolor='pink')
	plt.title("各种户型数量分析")
	plt.plot(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=45)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def houseBarView(df):
	counts = df["户型"].value_counts().sort_index()
	plt.figure(figsize=(10, 6))
	plt.bar(counts.index, counts.values, color="#b3c6ff")
	plt.xlabel("户型")
	plt.ylabel("数量")
	plt.title("户型统计")
	plt.xticks(rotation=45)
	plt.grid(axis="y")
	plt.show()


def directionView(df):
	counts = df.groupby(by="朝向").count()  # 统计
	plt.title("朝向统计")
	plt.bar(counts.index, counts["标题"], label="数量")
	plt.xticks(rotation=90)
	plt.ylabel("数量")
	plt.legend()
	plt.show()


def pingLunView(df):
	divide = [-1, 10, 30, 100, 1000, 1000000]
	cut = pd.cut(list(df["评论"]), divide)
	data = cut.describe()
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", startangle=45, radius=1)
	plt.legend(loc='best')
	plt.title("评论分布情况")
	plt.show()


def pingFengView(df):
	divide = [-1, 40, 42, 44, 46, 50]
	cut = pd.cut(list(df["评分"]), divide)
	data = cut.describe()
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", startangle=45, radius=1)
	plt.legend(loc='best')
	plt.title("评分分布情况")
	plt.show()


def priceView(df):
	divide = [-1, 0, 30, 70, 100, 10000]
	cut = pd.cut(list(df["价格"]), divide)
	data = cut.describe()
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", startangle=45, radius=1)
	plt.legend(loc='best')
	plt.title("价格分布情况")
	plt.show()


def reDuView(df):
	divide = [-1, 35, 40, 50, 60, 100]
	cut = pd.cut(list(df["热度"]), divide)
	data = cut.describe()
	print(data.index)
	print("-" * 100)
	print(data.counts.values)
	# 绘制扇形图
	plt.pie(x=data.freqs, labels=data.index, autopct="%1.2f%%", startangle=45, radius=1)
	plt.legend(loc='best')
	plt.title("热度分布情况")
	plt.show()


def doubleView(df):
	# 切分价格
	divide = [-1, 0, 30, 70, 100, 10000]
	df['价格区间'] = pd.cut(df['价格'], divide)
	reDu = df.groupby('价格区间', observed=True)['热度'].agg(list).reset_index()
	pingFeng = df.groupby('价格区间', observed=True)['评分'].agg(list).reset_index()
	reDuList = []
	pingFengList = []
	x = pingFeng['价格区间']
	for i in range(0, len(reDu['热度'])):
		reDuAverage = int(sum(reDu['热度'][i]) / len(reDu['热度'][i]))
		pingFengAverage = int(sum(pingFeng['评分'][i]) / len(pingFeng['评分'][i]))
		reDuList.append(reDuAverage)
		pingFengList.append(pingFengAverage)
	x = ['免费','(0, 30]','(30, 70]','(70, 100]','(100, 10000]']
	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.set_ylabel("热度")
	ax1.set_xlabel("价格区间（单位：元）")
	ax1.bar(x, reDuList, label="各价位区间热度平均值")
	ax2 = ax1.twinx()
	ax2.plot(x, pingFengList, label="各价位区间评分平均值", color="red")
	ax2.set_ylabel("评分")

	fig.legend(loc=1, bbox_to_anchor=(1, 1), bbox_transform=ax1.transAxes)  # 防止图例变形
	plt.show()


def maxReDuView(df):
	sort = df.sort_values("热度", ascending=False)
	x = sort.head(10)
	# 绘制扇形图
	plt.bar(x["名称"], x["热度"])
	plt.xticks(rotation=15)
	plt.title("热度最高的10大景点")
	plt.xlabel("名称")
	plt.ylabel("热度[0-100]")
	plt.show()


def maxPingFengView(df):
	sort = df.sort_values(["评论", "评分"], ascending=False)
	x = sort.head(10)
	# 绘制扇形图
	plt.bar(x["名称"], x["评分"])
	plt.xticks(rotation=15)
	plt.title("评分最高的10大景点")
	plt.xlabel("名称")
	plt.ylabel("评分[0-50]")
	plt.show()